const Koa = require("koa");
const { koaBody } = require("koa-body");
const cors = require("koa-cors");
const router = require("./app/router");
const { koaSwagger } = require('koa2-swagger-ui');
const swaggerSpec = require('swagger-jsdoc');
const bodyParser = require('koa-bodyparser');
const port = 8000
const app = new Koa();
const swaggerOptions = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Koa Swagger Example',
      version: '1.0.0',
      description: 'A simple example of using Swagger with Koa'
    },
    servers: [
      { url: `http://localhost:${port}` }
    ],
  },
  apis: ['./app/router.js'], // 您需要根据您的项目结构来定义这个选项
};

// 生成 swagger 规范
const swaggerSpecObj = swaggerSpec(swaggerOptions);
app.use(koaBody({ multipart: true }));
app.use(cors({ origin: "*" }));
app.use(bodyParser());
// 注册 swagger ui 中间件
app.use(koaSwagger({
  swaggerOptions: {
    spec: swaggerSpecObj, // 使用上面生成的 swagger 规范
  },
  routePrefix: '/swagger-ui', // 假设您希望将文档放在 /doc 路径下
}));

app.use(router);
app.listen(port, () =>
  console.log(`server listening on to http://localhost:${port}`)
);
